import os
import cgi
import datetime
import wsgiref.handlers

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from google.appengine.ext.webapp import template

#========== MODELS ==========
class Question(db.Model):
  User = db.UserProperty()
  Text = db.StringProperty()
  Answers = db.StringListProperty()
#========== MODELS ==========

#========== PAGES ==========
class MainPage(webapp.RequestHandler):
  def get(self):
    user = users.get_current_user()
    
    if user:
      #self.response.headers['Content-Type'] = 'text/plain'
      template_values = {"User": user.nickname()}
    else:
      template_values = {}
      self.redirect(users.create_login_url(self.request.uri))
    
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

class NewQuestion(webapp.RequestHandler):
  def get(self):
    user = users.get_current_user()
    
    if user:
      #self.response.headers['Content-Type'] = 'text/plain'
      template_values = {"User": user.nickname()}
    else:
      template_values = {}
      self.redirect(users.create_login_url(self.request.uri))
      
    path = os.path.join(os.path.dirname(__file__), 'newquestion.html')
    self.response.out.write(template.render(path, template_values))

class AddQuestion(webapp.RequestHandler):
  def post(self):
    user = users.get_current_user()
    
    if user:
      #self.response.headers['Content-Type'] = 'text/plain'
      template_values = {"User": user.nickname()}

      question = Question()
      
      question.User = user
      question.Text = self.request.get("question")
      question.Answers = [self.request.get("ans1"), self.request.get("ans2"), self.request.get("ans3")]
      question.put()
    else:
      template_values = {}
      self.redirect(users.create_login_url(self.request.uri))
      
    path = os.path.join(os.path.dirname(__file__), 'addquestion.html')
    self.response.out.write(template.render(path, template_values))

class CheckQuestion(webapp.RequestHandler):
  def get(self):
    user = users.get_current_user()
    
    if user:
      #self.response.headers['Content-Type'] = 'text/plain'
      questions = db.GqlQuery("SELECT * FROM Question WHERE User = :1", user)
      template_values = {"User": user, "Questions": questions}
      #question = Question()
      
      #for q in questions:
        #question = q
        
      #template_values = {"User": user.nickname(), "Question": question.Text, "Ans1": question.Answers[0], "Ans2": question.Answers[1], "Ans3": question.Answers[2]}
    else:
      template_values = {}
      self.redirect(users.create_login_url(self.request.uri))
      
    path = os.path.join(os.path.dirname(__file__), 'checkquestion.html')
    self.response.out.write(template.render(path, template_values))
#========== PAGES ==========

application = webapp.WSGIApplication([
  ('/', MainPage),
  ('/newquestion', NewQuestion),
  ('/addquestion', AddQuestion),
  ('/checkquestion', CheckQuestion)
], debug=True)


def main():
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
